<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="开始时间:">
              <a-date-picker v-model:value="where.searchBeginTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
            <a-form-item label="结束时间:">
              <a-date-picker v-model:value="where.searchEndTime" type="date" show-time format="YYYY-MM-DD HH:mm:ss" valueFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="logId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table操作栏按钮 -->
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="viewTimeslot(record)">时间段配置</a>
                <a-divider type="vertical" />
                <a @click="viewDatahuaTimeSlot(record)">时间点配置</a>
                <a-divider type="vertical" />
                <a @click="viewRunmessage(record)">查看报文</a>
                <a-divider type="vertical" v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_SPRAYRULE_SET_LOG_DEL']" />
                <div v-permission="['PROPERTY_DEVICELIST_MGR_SPRAYCAMERA_CONFIG_SPRAYRULE_SET_LOG_DEL']">
                   <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                      <a class="ele-text-danger">删除</a>
                    </a-popconfirm>
                </div>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <!-- Timeslot数据弹窗 -->
    <a-modal
      v-model:visible="timeslotModalVisible"
      title="时间段配置"
      width="800px"
      :footer="null"
    >
      <a-card :bordered="false">
        <pre class="json-viewer">{{ formatJson(timeslotData) }}</pre>
      </a-card>
    </a-modal>

     <a-modal
      v-model:visible="dahuaTimeslotModalVisible"
      title="时间点配置"
      width="800px"
      :footer="null"
    >
      <a-card :bordered="false">
        <pre class="json-viewer">{{ formatJson(dahuaTimeslotData) }}</pre>
      </a-card>
    </a-modal>

    <!-- Timeslot数据弹窗 -->
    <a-modal
      v-model:visible="viewRunmessageModalVisible"
      title="推送详细报文"
      width="800px"
      :footer="null"
    >
      <a-card :bordered="false">
        <pre class="json-viewer">{{ formatJson(runmessageData) }}</pre>
      </a-card>
    </a-modal>
  </div>
</template>

<script>
import {
  DeviceSprayCameraSprayruleupdateLogApi
} from "@/api/property/spraycamera/DeviceSprayCameraSprayruleupdateLogApi";
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceSpraycameraSprayruleupdateLog',
  components: {
  },
  props: {
    data: Object
  },
  async mounted() {
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          fixed: 'left'
        },
        {
          title: '喷淋模式',
          dataIndex: 'mode'
        },
        {
          title: '识别容忍度(秒)',
          dataIndex: 'identify'
        },
        {
          title: '喷淋时长(秒)',
          dataIndex: 'spray'
        },
        {
          title: '间隔时间(秒)',
          dataIndex: 'sprayInterval'
        },
        {
          title: '时间版本',
          dataIndex: 'timeSlotVersion'
        },
        {
          title: '触发类型',
          dataIndex: 'eventType',
          customRender: function(row) {
            if (row.text == 1) {
              return "时间段触发";
            } else if (row.text == 2) {
              return "温度触发";
            } else if (row.text == 3) {
              return "默认配置同步触发";
            }
          }
        },
        {
          title: '推送情况',
          dataIndex: 'sendStatus',
          customRender: function(row) {
            const isSend = row.text;
            if (isSend == 0) {
              return '未推送';
            } else if (isSend == 1) {
              return '推送中';
            } else if (isSend == 2) {
              return '已推送等反馈';
            } else if (isSend == 3) {
              return '已反馈';
            } else if (isSend == 4) {
              return '失败';
            }
          }
        },
        {
          title: '报错信息',
          dataIndex: 'sendMessage'
        },
        {
          title: '操作',
          key: 'action',
          width: 260,
          align: 'center',
        }
      ],
      // 表格搜索条件
      where: {"recordId" : this.data.recordId},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      timeslotModalVisible: false,
      timeslotData: {},
      viewRunmessageModalVisible : false,
      runmessageData : {},
      dahuaTimeslotModalVisible : false,
      dahuaTimeslotData : {},
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2025/04/06 10:11
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2025/04/06 10:11
     */
    reset() {
      this.where.recordId = this.data.recordId;
      this.where.searchBeginTime = '';
      this.where.searchEndTime = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2025/04/06 10:11
     */
    async remove(row) {
      const result = await DeviceSprayCameraSprayruleupdateLogApi.delete({ logId: row.logId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2025/04/06 10:11
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    datasource({ page, limit, where, orders }) {
      return DeviceSprayCameraSprayruleupdateLogApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },
    /**
     * 查看Timeslot数据
     */
    async viewTimeslot(record) {
      this.timeslotModalVisible = true;
      try {
        this.timeslotData = JSON.parse(record.timeslot || '{}');
      } catch (e) {
        this.timeslotData = record.timeslot || '';
        console.error('JSON解析失败:', e);
      }
    },
    /**
     * 查看Timeslot数据
     */
     async viewRunmessage(record) {
      this.viewRunmessageModalVisible = true;
      try {
        this.runmessageData = JSON.parse(record.runmessage || '{}');
      } catch (e) {
        this.runmessageData = record.runmessage || '';
        console.error('JSON解析失败:', e);
      }
    },

    /**
     * 查看Timeslot数据
     */
     async viewDatahuaTimeSlot(record) {
      this.dahuaTimeslotModalVisible = true;
      try {
        this.dahuaTimeslotData = JSON.parse(record.dahuaTimeslot || '{}');
      } catch (e) {
        this.dahuaTimeslotData = record.dahuaTimeslot || '';
        console.error('JSON解析失败:', e);
      }
    },

    /**
     * 格式化JSON数据
     */
    formatJson(json) {
      try {
        return JSON.stringify(json, null, 2);
      } catch (e) {
        return JSON.stringify(json);
      }
    }
  }
};
</script>

<style scoped>
.json-viewer {
  background-color: #f5f5f5;
  padding: 16px;
  border-radius: 4px;
  max-height: 500px;
  overflow: auto;
  font-family: 'Courier New', Courier, monospace;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>
